package com.juyu.miliao.api.dao.redis;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.juyu.miliao.api.dao.mysql.QuestionAndAnswerInfoDao;
import com.juyu.miliao.api.domain.QuestionAndAnswerInfo;
import com.juyu.miliao.common.constant.QuestionAndAnswerInfoRedisKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @author xuanqicheng
 * @create 2018-18-4:18 PM
 */
@Component
public class QuestionAndAnswerInfoRedisDao {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private QuestionAndAnswerInfoDao questionAndAnswerInfoDao;


    public List<QuestionAndAnswerInfo> selectVipVoiceList(Map<String, Object> parameterMap) {
        return questionAndAnswerInfoDao.selectVipVoiceList(parameterMap);
    }

    public QuestionAndAnswerInfo getVoiceByQuestionId(Long questionId) {

        String key = QuestionAndAnswerInfoRedisKey.QuestionAndAndwserInfoByQuestionIdEnum.REDIS_KEY
                .getKeyFormat(Long.toString(questionId));


        String temp = stringRedisTemplate.opsForValue().get(key);

        if (!StringUtils.isEmpty(temp)) {
            QuestionAndAnswerInfo obj = JSONObject.parseObject(temp, QuestionAndAnswerInfo.class);
            return obj;
        } else {
            QuestionAndAnswerInfo obj = questionAndAnswerInfoDao.selectOne(questionId);
            if (obj != null) {
                stringRedisTemplate.opsForValue().set(key, JSONObject.toJSONString(obj), 15, TimeUnit.HOURS);
            }
            return obj;
        }

    }

    public List<QuestionAndAnswerInfo> selectSecretVoiceByAnchorList(Map<String, Object> parameterMap) {
        List<QuestionAndAnswerInfo> list = questionAndAnswerInfoDao.selectSecretVoiceByAnchorList(parameterMap);
        return list;
    }

    public List<QuestionAndAnswerInfo> selecrSecretVoiceByNotAnchorList(Map<String, Object> parameterMap) {
        List<QuestionAndAnswerInfo> list = questionAndAnswerInfoDao.selecrSecretVoiceByNotAnchorList(parameterMap);
        return list;
    }

    /**
     * 获取偷听数
     *
     * @param questionId
     * @return
     */
    public long getAnswerEavesdroppingNumber(long questionId) {

        String key = QuestionAndAnswerInfoRedisKey.QuestionAndAndwserEavesdroppedNumberEnum.REDIS_KEY
                .getKeyFormat(Long.toString(questionId));
        long number = 0;
        String temp = stringRedisTemplate.opsForValue().get(key);

        if (!StringUtils.isEmpty(temp)) {

            number = Long.parseLong(temp);
        }
        return number;
    }

    /**
     * 更新偷听数
     *
     * @param questionId
     * @param userId
     * @return
     */
    public void setAnswerEavesdroppingNumber(long questionId, long userId) {

        String key = QuestionAndAnswerInfoRedisKey.QuestionAndAndwserEavesdropedUserEnum.REDIS_KEY
                .getKeyFormat(Long.toString(questionId));

        boolean bl = stringRedisTemplate.opsForSet().isMember(key, userId + "");

        if (!bl) {
            stringRedisTemplate.opsForSet().add(key, userId + "");

            String numberKey = QuestionAndAnswerInfoRedisKey.QuestionAndAndwserEavesdroppedNumberEnum.REDIS_KEY
                    .getKeyFormat(Long.toString(questionId));
            stringRedisTemplate.opsForValue().increment(numberKey, 1);

        }


    }

    public List<QuestionAndAnswerInfo> selectListByAnchor(Map<String, Object> parameterMap) {

        return questionAndAnswerInfoDao.selectListByAnchor(parameterMap);
    }

    public List<QuestionAndAnswerInfo> selectListByAll(Map<String, Object> parameterMap) {
        return questionAndAnswerInfoDao.selectListByAll(parameterMap);
    }

    /**
     * 获取优质问题列表
     */
    public List<String> getQualityQuestionAndAnswerList(){

        String key = QuestionAndAnswerInfoRedisKey.QuestionAndAndwserByHighQuality.REDIS_KEY.getKeyFormat();
        String temp = stringRedisTemplate.opsForValue().get(key);

        if(!StringUtils.isEmpty(temp)){
            List<String> list = JSONObject.parseArray(temp, String.class);
            return list;
        }else{
            List<String> list = questionAndAnswerInfoDao.selectQualityQuestionAndAnswerIdList();
            if(list.size()>0){
                stringRedisTemplate.opsForValue().set(key,JSONObject.toJSONString(list),30,TimeUnit.MINUTES);
            }

            return list;

        }

    }

}
